﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;

namespace Things.Common
{
    public class UICommon
    {
        public static void BindDDL(DropDownList ddl, DataSet ds, string textFieldName, string valueFieldName)
        {
            if (ddl == null || ds == null || ds.Tables.Count <= 0
                || string.IsNullOrEmpty(textFieldName) || string.IsNullOrEmpty(valueFieldName))
            {
                return;
            }

            ddl.Items.Clear();
            ddl.Items.Add(new ListItem(" -- 请选择 -- ", "-1"));

            try
            {             
                DataTable dt = ds.Tables[0];
                DataRowCollection rows = dt.Rows;
                foreach (DataRow row in rows)
                {
                    ddl.Items.Add(new ListItem(row[textFieldName].ToString(), row[valueFieldName].ToString()));
                }
            }
            catch
            {
            }
        }

        public static void SetSelectedDDL(DropDownList ddl, string selectedValue)
        {
            if (ddl == null || string.IsNullOrEmpty(selectedValue))
            {
                return;
            }

            ListItem item = ddl.Items.FindByValue(selectedValue);
            if (item != null)
            {
                ddl.ClearSelection();
                item.Selected = true;
            }
        }

        public static void BindDDL(DropDownList ddl, DataSet ds, string textFieldNames,string seperate, string valueFieldName)
        {
            if (ddl == null || ds == null || ds.Tables.Count <= 0
                || string.IsNullOrEmpty(textFieldNames) || string.IsNullOrEmpty(seperate) || string.IsNullOrEmpty(valueFieldName))
            {
                return;
            }

            ddl.Items.Clear();
            ddl.Items.Add(new ListItem(" -- 请选择 -- ", "-1"));

            try
            {
                DataTable dt = ds.Tables[0];
                DataRowCollection rows = dt.Rows;
                List<string> fields = textFieldNames.Split(',').ToList<string>();
                
                foreach (DataRow row in rows)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (string filed in fields)
                    {
                        sb.Append(row[filed].ToString());
                        sb.Append(seperate);
                    }
                    sb.Remove(sb.Length - seperate.Length, seperate.Length);
                    ddl.Items.Add(new ListItem(sb.ToString(), row[valueFieldName].ToString()));                    
                }
            }
            catch
            {
            }
        }

        public static byte[] GetImages(FileUpload FileUpload1)
        {
            if (string.IsNullOrEmpty(FileUpload1.FileName) == false)
            {
                string name = FileUpload1.PostedFile.FileName;
                string type = name.Substring(name.LastIndexOf(".") + 1);
                //FileStream fs = File.OpenRead(name);
                //byte[] content = new byte[fs.Length];
                //fs.Read(content, 0, content.Length);
                //fs.Close();

                byte[] content = FileUpload1.FileBytes;

                if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
                {
                    return content;
                }
            }

            return new byte[] { };

        }

        public static string ConvertBooleanToString(bool value)
        {
            if (value == true)
            {
                return "是";
            }
            else
            {
                return "否";
            }
        }


        public static void BindDDLTree(DropDownList ddl, DataTable dt, Guid CurrentParentID, string separator)
        {

            var quer = dt.Select("ParentID='" + CurrentParentID.ToString() + "'");
          
            foreach (var item in quer)
            {
                ListItem li = new ListItem();
                li.Text = HttpUtility.HtmlDecode(separator + item["GoodsCategoryName"].ToString());
                li.Value = item["ID"].ToString();
                ddl.Items.Add(li);
                string separator_ = separator + separator;
                Guid parentID= Guid.Empty;
                try
                {
                    parentID=new Guid(item["ID"].ToString());
                    BindDDLTree(ddl, dt, parentID, separator_);
                }
                catch
                {

                }                
            }
        }

    }
}
